package com.leetcode.code;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.Semaphore;

/**
 * Description:
 *
 * @author zkingcobra
 * Date: 2020/6/9 11:13 上午
 */
public class LeetCode1117Java {

    public static void main(String[] args) {

        Runnable releaseHydrogen = new Runnable() {
            @Override public void run() {
                System.out.println("H");
            }
        };

        Runnable releaseOxygen = new Runnable() {
            @Override public void run() {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("O");
            }
        };

        H2O2 h2O = new H2O2();

        new Thread(() -> {
            try {
                h2O.hydrogen(releaseHydrogen);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).start();

        new Thread(() -> {
            try {
                h2O.hydrogen(releaseHydrogen);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).start();

        new Thread(() -> {
            try {
                h2O.oxygen(releaseOxygen);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).start();
    }

    static class H2O {
        private final Semaphore h = new Semaphore(2);
        private final Semaphore o = new Semaphore(0);

        public H2O() {
        }

        public void hydrogen(Runnable releaseHydrogen) throws InterruptedException {
            h.acquire();
            releaseHydrogen.run();
            o.release();
        }

        public void oxygen(Runnable releaseOxygen) throws InterruptedException {
            o.acquire(2);
            releaseOxygen.run();
            h.release(2);
        }
    }

    /**
     * CyclicBarrier 和第一种方式效率差不多
     */
    static class H2O2 {
        private final Semaphore semaphoreH = new Semaphore(2);
        private final Semaphore semaphoreO = new Semaphore(1);
        private final CyclicBarrier barrier = new CyclicBarrier(3);

        public H2O2() {

        }

        public void hydrogen(Runnable releaseHydrogen) throws InterruptedException {
            semaphoreH.acquire();
            try {
                barrier.await();
            } catch (BrokenBarrierException e) {
                e.printStackTrace();
            }
            // releaseHydrogen.run() outputs "H". Do not change or remove this line.
            releaseHydrogen.run();
            semaphoreH.release();
        }

        public void oxygen(Runnable releaseOxygen) throws InterruptedException {
            semaphoreO.acquire();
            try {
                barrier.await();
            } catch (BrokenBarrierException e) {
                e.printStackTrace();
            }
            // releaseOxygen.run() outputs "H". Do not change or remove this line.
            releaseOxygen.run();
            semaphoreO.release();
        }
    }
}
